package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.maps2d.model.LatLng;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.ParkBean;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.MainFragInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/14.
 */
public class MainFragInteractorImp extends NetWorkImp implements MainFragInteractor {

    RequestListener<List<ParkBean>> listener;
    LatLng[] la = new LatLng[]{new LatLng(34.7466, 113.625367), new LatLng(34.7446, 113.625367), new LatLng(34.7416, 113.621367), new LatLng(34.7406, 113.629367)};

    public MainFragInteractorImp(Context c, RequestListener<List<ParkBean>> listener) {
        super(c);
        this.listener = listener;
    }

    @Override
    public void getParking(LatLng latLng, String sessionId, int currentPage) {
        getParking(latLng, sessionId, currentPage, MethodCode.PAGE_SIZE);
    }

    @Override
    public void getParking(LatLng latLng, String sessionId, int currentPage, int pageSize) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("lat",
//                latLng.latitude
                34.8227600000);
        params.put("lng",
//                latLng.longitude
                113.6424760000
        );
        bean.setRequestParams(params);
        bean.setUserSession(sessionId);
        bean.setRequestContentCurrentPage(currentPage);
        bean.setRequestCode(MethodType.GET_PARK);
        addFastJsonQueue(Request.Method.POST, MethodType.GET_PARK, null, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    public void onRequestSuccess(Object tag, JSONObject result) {
        List<ParkBean> parks = new ArrayList<>();
//        for (LatLng l : la) {
//            ParkBean bean = new ParkBean();
//            bean.setLat(l.latitude);
//            bean.setLng(l.longitude);
//            bean.setParkId("123");
//            parks.add(bean);
//        }
//        listener.Success(tag + "", parks);

        if (result == null){
            onRequestExcption(null);
            return;
        }
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
            onRequestExcption(null);
        } else {
            int  statue = response.getInteger(MethodCode.responseStatue);

            if (statue == (MethodCode.NetState.SUCCESS.getValue()))

                if (tag.toString().equals(MethodType.GET_PARK)) {
                    JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                    JSONArray parking = obj.getJSONArray("parking");

                    for (int i = 0; i < parking.size(); i++) {
                        JSONObject p = parking.getJSONObject(i);
                        if (p != null) {
                            ParkBean bean = JSONObject.toJavaObject(p, ParkBean.class);
                            parks.add(bean);
                        }
                    }
                    listener.Success(tag + "", parks);
                } else
                    listener.Error(tag + "", "获取停车场位置失败");
        }

    }

    @Override
    protected void onRequestExcption(VolleyError error) {

        if (error == null) {
            log.v(this.getClass().getName(), "错误内容为空");
            return;
        }
        listener.Exception(tag, null);
    }
}
